package com_7;

import java.io.*;
import java.net.Socket;

public class SocketThead implements Runnable {//定义一个实现了ranable接口的类
    private Socket sc;//定义变量，因为要使用socket对象，所以将他作为变量
    public SocketThead(Socket sc) {//本类的有参数构造方法
        this.sc = sc;//将服务端传来的socket对象赋值给本类的变量
    }
    @Override
    public void run() {//重写Ranable接口中个方法法
        while (true) {//使用while死循环进行监听客户端的
            try {//
                BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
                //
                int count = 0;//
                File f = new File("UDP\\x.[" + count + "]text");//
                while (f.exists()) {//
                    count++;//
                    f = new File("UDP\\x.[" + count + "]text");//
                }
                BufferedWriter bw = new BufferedWriter(new FileWriter(f));//
                String line;//
                while ((line = br.readLine()) != null) {//
                    bw.write(line);//
                    bw.newLine();//
                    bw.flush();//
                }
                br.close();//
                bw.close();//
            } catch (IOException e) {//
                e.printStackTrace();//
            }
        }
    }
}
